Refactor build script output state initialization
authorAlex Crichton <alex@alexcrichton.com>
Fri, 19 Feb 2016 19:15:39 +0000 (11:15 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 19 Feb 2016 19:15:39 +0000 (11:15 -0800)
Like with the previous refactor, remove the need to list all packages in a
package set as we only need to lazily do this for the actual packages being
compiled. Whenever a build script is requested to be executed is when we
actually go and try to see if an override was in play.

src/cargo/ops/cargo_rustc/context.rs
src/cargo/ops/cargo_rustc/custom_build.rs

index a3d7fbd7beabe6eaa9fe7bc74d8113947b55ab5d..28b150d2b3b604db8141530b7644d46188e2f7c5 100644 (file)
@@ -88,7 +88,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
             target_info: target_info,
             host_info: host_info,
             compilation: Compilation::new(config),
-            build_state: Arc::new(BuildState::new(&build_config, packages)),
+            build_state: Arc::new(BuildState::new(&build_config)),
             build_config: build_config,
             exec_engine: engine,
             fingerprints: HashMap::new(),
index 552b2d175964fe0fd9ab7eb80f09377da9dd6de9..e52a12c402619dc89c0ccea2af253386773c8eb9 100644 (file)
@@ -5,7 +5,7 @@ use std::path::{PathBuf, Path};
 use std::str;
 use std::sync::{Mutex, Arc};
 
-use core::{PackageId, PackageSet};
+use core::PackageId;
 use util::{CargoResult, human, Human};
 use util::{internal, ChainError, profile, paths};
 use util::Freshness;
@@ -33,6 +33,7 @@ pub type BuildMap = HashMap<(PackageId, Kind), BuildOutput>;
 
 pub struct BuildState {
     pub outputs: Mutex<BuildMap>,
+    overrides: HashMap<(String, Kind), BuildOutput>,
 }
 
 #[derive(Default)]
@@ -65,8 +66,7 @@ pub fn prepare<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>)
                          -> CargoResult<(Work, Work, Freshness)> {
     let _p = profile::start(format!("build script prepare: {}/{}",
                                     unit.pkg, unit.target.name()));
-    let key = (unit.pkg.package_id().clone(), unit.kind);
-    let overridden = cx.build_state.outputs.lock().unwrap().contains_key(&key);
+    let overridden = cx.build_state.has_override(unit);
     let (work_dirty, work_fresh) = if overridden {
         (Work::new(|_| Ok(())), Work::new(|_| Ok(())))
     } else {
@@ -238,34 +238,34 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>)
 }
 
 impl BuildState {
-    pub fn new(config: &super::BuildConfig,
-               packages: &PackageSet) -> BuildState {
-        let mut sources = HashMap::new();
-        for package in packages.packages() {
-            match package.manifest().links() {
-                Some(links) => {
-                    sources.insert(links.to_string(),
-                                   package.package_id().clone());
-                }
-                None => {}
-            }
-        }
-        let mut outputs = HashMap::new();
+    pub fn new(config: &super::BuildConfig) -> BuildState {
+        let mut overrides = HashMap::new();
         let i1 = config.host.overrides.iter().map(|p| (p, Kind::Host));
         let i2 = config.target.overrides.iter().map(|p| (p, Kind::Target));
         for ((name, output), kind) in i1.chain(i2) {
-            // If no package is using the library named `name`, then this is
-            // just an override that we ignore.
-            if let Some(id) = sources.get(name) {
-                outputs.insert((id.clone(), kind), output.clone());
-            }
+            overrides.insert((name.clone(), kind), output.clone());
+        }
+        BuildState {
+            outputs: Mutex::new(HashMap::new()),
+            overrides: overrides,
         }
-        BuildState { outputs: Mutex::new(outputs) }
     }
 
     fn insert(&self, id: PackageId, kind: Kind, output: BuildOutput) {
         self.outputs.lock().unwrap().insert((id, kind), output);
     }
+
+    fn has_override(&self, unit: &Unit) -> bool {
+        let key = unit.pkg.manifest().links().map(|l| (l.to_string(), unit.kind));
+        match key.and_then(|k| self.overrides.get(&k)) {
+            Some(output) => {
+                self.insert(unit.pkg.package_id().clone(), unit.kind,
+                            output.clone());
+                true
+            }
+            None => false,
+        }
+    }
 }
 
 impl BuildOutput {